package cz.cvut.manatee.client.concordance;

import java.util.ArrayList;
import java.util.Date;

import com.google.gwt.user.client.Window;

import cz.cvut.manatee.shared.concordance.ResultTableRow;
import cz.cvut.manatee.shared.concordance.ResultTableWord;

/**
 * The Class ResultsTableFeeder is MOCK OBJECT at this time.
 */
public class ResultsTableSource {
	
	private ArrayList<ResultTableRow> linesArray;

	private int maxLeftContext = 0;
	private int maxRightContext = 0;
	private int kwicCount = 0;
	private int attrCount = 0;
	
	public ResultsTableSource(){
		linesArray = new ArrayList<ResultTableRow>();
		
		/*
		 * toto je zde pouze pro MOCK cast
		 * jinak se tento konstruktor pouzivat nebude
		 */
		Date date = new Date();
        int rand = (int)date.getTime() %100;
        
		String [] slova = {"nefunguje","téměř","nic",",","a","že","tedy","ani","policejní","psi","nemají","řádný","výcvik",",","nedošlo","k","žádnému"};
		int line, word;

		for(line=0;line<10;line++){

			String [] attributArray = new String[3];
			ResultTableWord [] leftsArray = new ResultTableWord[3];
			ResultTableWord [] kwicssArray = new ResultTableWord[3];
			ResultTableWord [] rightsArray = new ResultTableWord[3];

			for(word=0;word<attributArray.length;word++){
				attributArray[word] = new String(slova[(rand++)%slova.length]);
			}

			for(word=0;word<leftsArray.length;word++){
				leftsArray[word] = new ResultTableWord(slova[(rand++)%slova.length], "lemma", "TAG");
			}

			for(word=0;word<kwicssArray.length;word++){
				kwicssArray[word] = new ResultTableWord(slova[(rand++)%slova.length], "lemma", "TAG");
			}

			for(word=0;word<rightsArray.length;word++){
				rightsArray[word] = new ResultTableWord(slova[(rand++)%slova.length], "lemma", "TAG");
			}
			
			linesArray.add( new ResultTableRow(attributArray, leftsArray, kwicssArray, rightsArray) );
		}
		
		maxLeftContext = 5;
		maxRightContext = 5;
		kwicCount = 1;
		attrCount = 1;
	}
	
	public ResultsTableSource(ResultTableRow[] data){
		linesArray = new ArrayList<ResultTableRow>();
		
		if(data != null){
			for(ResultTableRow row : data){
				linesArray.add(row);
				attrCount = Math.max(attrCount, row.getAttributs().length);
				kwicCount = Math.max(kwicCount, row.getKwics().length);
				maxLeftContext = Math.max(maxLeftContext, row.getLeftContexts().length);
				maxRightContext = Math.max(maxRightContext, row.getRightContexts().length);
			}
		}
		else Window.alert("Ze serveru přišel NULL");
	}

	public int getMaxLeftContext(){
		return maxLeftContext;
	}

	public int getMaxRightContext(){
		return maxRightContext;
	}

	public int getKwicCount(){
		return kwicCount;
	}

	public int getAttrCount(){
		return attrCount;
	}

	public int getRowsCount(){
		return linesArray.size();
	}
	
	public ArrayList<ResultTableRow> getData(){

		return linesArray;
		
	}
	
	public void addSourceData(ResultsTableSource source){
		
		for(ResultTableRow row : source.linesArray){
			linesArray.add(row);
		}
		
	}
}
